/* (c) 2012 Jungo Ltd. All Rights Reserved. Jungo Confidential */
#include <jos.h>
#include <bluetooth.h>
#include <bt_api.h>
#include <bt_sdp.h>
#include <bt_sdp_types.h>
#include "bt_app_internal.h"
#include "app_Mobvoi_includes.h"
#include "bt_hid.h"

#define KEY_ENTER 0
#define KEY_VOL_INC 1

typedef struct
{
    uint32_t svc_id;
    uint32_t server_sdp;
    uint8_t last_tid;
    uint32_t attr_id;
    uint16_t char_set_id;

    BOOL is_used;

    uint8_t last_cmd;
} hid_backend_t;

static sdp_hid_profile_t hid_profile_ct = {
    BT_SVC_TYPE_HUMAN_INTERFACE_DEVICE, /* Control or Target */
    "HID comp"                          /* Service name */
};

static uint32_t *testHandle;
static hid_backend_t g_app_hid;
static uint8_t photo_key;
uint8_t hid_mode = 0; // 1:HID connected before taking photo ;   0:HID always on

CONST uint8_t MA_List[][3] = {
    {0x00, 0x03, 0x93}, //apple
    {0x00, 0x05, 0x02},
    {0x00, 0x0A, 0x27},
    {0x00, 0x0A, 0x95},
    {0x00, 0x0D, 0x93},
    {0x00, 0x10, 0xFA},
    {0x00, 0x11, 0x24},
    {0x00, 0x14, 0x51},
    {0x00, 0x16, 0xCB},
    {0x00, 0x17, 0xF2},
    {0x00, 0x19, 0xE3},
    {0x00, 0x1B, 0x63},
    {0x00, 0x1C, 0xB3},
    {0x00, 0x1D, 0x4F},
    {0x00, 0x1E, 0x52},
    {0x00, 0x1E, 0xC2},
    {0x00, 0x1F, 0x5B},
    {0x00, 0x1F, 0xF3},
    {0x00, 0x21, 0xE9},
    {0x00, 0x22, 0x41},
    {0x00, 0x23, 0x12},
    {0x00, 0x23, 0x32},
    {0x00, 0x23, 0x6C},
    {0x00, 0x23, 0xDF},
    {0x00, 0x24, 0x36},
    {0x00, 0x25, 0x00},
    {0x00, 0x25, 0x4B},
    {0x00, 0x25, 0xBC},
    {0x00, 0x26, 0x08},
    {0x00, 0x26, 0x4A},
    {0x00, 0x26, 0xB0},
    {0x00, 0x26, 0xBB},
    {0x00, 0x30, 0x65},
    {0x00, 0x3E, 0xE1},
    {0x00, 0x50, 0xE4},
    {0x00, 0x61, 0x71},
    {0x00, 0x88, 0x65},
    {0x00, 0xA0, 0x40},
    {0x00, 0xC6, 0x10},
    {0x00, 0xF4, 0xB9},
    {0x00, 0xF7, 0x6F},
    {0x04, 0x0C, 0xCE},
    {0x04, 0x15, 0x52},
    {0x04, 0x1E, 0x64},
    {0x04, 0x26, 0x65},
    {0x04, 0x48, 0x9A},
    {0x04, 0x54, 0x53},
    {0x04, 0xDB, 0x56},
    {0x04, 0xE5, 0x36},
    {0x04, 0xF1, 0x3E},
    {0x04, 0xF7, 0xE4},
    {0x08, 0x00, 0x07},
    {0x08, 0x70, 0x45},
    {0x0C, 0x30, 0x21},
    {0x0C, 0x3E, 0x9F},
    {0x0C, 0x4D, 0xE9},
    {0x0C, 0x74, 0xC2},
    {0x0C, 0x77, 0x1A},
    {0x10, 0x1C, 0x0C},
    {0x10, 0x40, 0xF3},
    {0x10, 0x93, 0xE9},
    {0x10, 0x9A, 0xDD},
    {0x10, 0xDD, 0xB1},
    {0x14, 0x10, 0x9F},
    {0x14, 0x5A, 0x05},
    {0x14, 0x8F, 0xC6},
    {0x14, 0x99, 0xE2},
    {0x18, 0x20, 0x32},
    {0x18, 0x34, 0x51},
    {0x18, 0x9E, 0xFC},
    {0x18, 0xAF, 0x61},
    {0x18, 0xAF, 0x8F},
    {0x18, 0xE7, 0xF4},
    {0x1C, 0x1A, 0xC0},
    {0x1C, 0xAB, 0xA7},
    {0x1C, 0xE6, 0x2B},
    {0x20, 0x7D, 0x74},
    {0x20, 0xA2, 0xE4},
    {0x20, 0xC9, 0xD0},
    {0x24, 0xA0, 0x74},
    {0x24, 0xA2, 0xE1},
    {0x24, 0xAB, 0x81},
    {0x24, 0xE3, 0x14},
    {0x28, 0x0B, 0x5C},
    {0x28, 0x37, 0x37},
    {0x28, 0x6A, 0xB8},
    {0x28, 0x6A, 0xBA},
    {0x28, 0xCF, 0xDA},
    {0x28, 0xCF, 0xE9},
    {0x28, 0xE0, 0x2C},
    {0x28, 0xE1, 0x4C},
    {0x28, 0xE7, 0xCF},
    {0x2C, 0xB4, 0x3A},
    {0x2C, 0xBE, 0x08},
    {0x2C, 0xF0, 0xEE},
    {0x30, 0x10, 0xE4},
    {0x30, 0x90, 0xAB},
    {0x30, 0xF7, 0xC5},
    {0x34, 0x15, 0x9E},
    {0x34, 0x51, 0xC9},
    {0x34, 0xA3, 0x95},
    {0x34, 0xC0, 0x59},
    {0x34, 0xE2, 0xFD},
    {0x38, 0x0F, 0x4A},
    {0x38, 0x48, 0x4C},
    {0x3C, 0x07, 0x54},
    {0x3C, 0x15, 0xC2},
    {0x3C, 0xAB, 0x8E},
    {0x3C, 0xD0, 0xF8},
    {0x3C, 0xE0, 0x72},
    {0x40, 0x30, 0x04},
    {0x40, 0x3C, 0xFC},
    {0x40, 0x6C, 0x8F},
    {0x40, 0xA6, 0xD9},
    {0x40, 0xB3, 0x95},
    {0x40, 0xD3, 0x2D},
    {0x44, 0x2A, 0x60},
    {0x44, 0x4C, 0x0C},
    {0x44, 0xD8, 0x84},
    {0x44, 0xFB, 0x42},
    {0x48, 0x43, 0x7C},
    {0x48, 0x60, 0xBC},
    {0x48, 0x74, 0x6E},
    {0x48, 0xD7, 0x05},
    {0x4C, 0x8D, 0x79},
    {0x4C, 0xB1, 0x99},
    {0x50, 0xEA, 0xD6},
    {0x54, 0x26, 0x96},
    {0x54, 0x72, 0x4F},
    {0x54, 0xAE, 0x27},
    {0x54, 0xE4, 0x3A},
    {0x54, 0xEA, 0xA8},
    {0x58, 0x1F, 0xAA},
    {0x58, 0x55, 0xCA},
    {0x58, 0xB0, 0x35},
    {0x5C, 0x59, 0x48},
    {0x5C, 0x8D, 0x4E},
    {0x5C, 0x95, 0xAE},
    {0x5C, 0x96, 0x9D},
    {0x5C, 0x97, 0xF3},
    {0x5C, 0xF9, 0x38},
    {0x60, 0x03, 0x08},
    {0x60, 0x33, 0x4B},
    {0x60, 0x69, 0x44},
    {0x60, 0x92, 0x17},
    {0x60, 0xC5, 0x47},
    {0x60, 0xD9, 0xC7},
    {0x60, 0xF8, 0x1D},
    {0x60, 0xFA, 0xCD},
    {0x60, 0xFB, 0x42},
    {0x60, 0xFE, 0xC5},
    {0x64, 0x20, 0x0C},
    {0x64, 0x76, 0xBA},
    {0x64, 0xA3, 0xCB},
    {0x64, 0xB9, 0xE8},
    {0x64, 0xE6, 0x82},
    {0x68, 0x09, 0x27},
    {0x68, 0x5B, 0x35},
    {0x68, 0x96, 0x7B},
    {0x68, 0x9C, 0x70},
    {0x68, 0xA8, 0x6D},
    {0x68, 0xAE, 0x20},
    {0x68, 0xD9, 0x3C},
    {0x6C, 0x3E, 0x6D},
    {0x6C, 0x40, 0x08},
    {0x6C, 0x70, 0x9F},
    {0x6C, 0x94, 0xF8},
    {0x6C, 0xC2, 0x6B},
    {0x70, 0x11, 0x24},
    {0x70, 0x3E, 0xAC},
    {0x70, 0x56, 0x81},
    {0x70, 0x73, 0xCB},
    {0x70, 0xCD, 0x60},
    {0x70, 0xDE, 0xE2},
    {0x74, 0xE1, 0xB6},
    {0x74, 0xE2, 0xF5},
    {0x78, 0x31, 0xC1},
    {0x78, 0x3A, 0x84},
    {0x78, 0x6C, 0x1C},
    {0x78, 0x7E, 0x61},
    {0x78, 0xA3, 0xE4},
    {0x78, 0xCA, 0x39},
    {0x78, 0xFD, 0x94},
    {0x7C, 0x11, 0xBE},
    {0x7C, 0x6D, 0x62},
    {0x7C, 0x6D, 0xF8},
    {0x7C, 0xC3, 0xA1},
    {0x7C, 0xC5, 0x37},
    {0x7C, 0xD1, 0xC3},
    {0x7C, 0xF0, 0x5F},
    {0x7C, 0xFA, 0xDF},
    {0x80, 0x00, 0x6E},
    {0x80, 0x49, 0x71},
    {0x80, 0x92, 0x9F},
    {0x80, 0xBE, 0x05},
    {0x80, 0xE6, 0x50},
    {0x80, 0xEA, 0x96},
    {0x84, 0x29, 0x99},
    {0x84, 0x38, 0x35},
    {0x84, 0x78, 0x8B},
    {0x84, 0x85, 0x06},
    {0x84, 0x8E, 0x0C},
    {0x84, 0xB1, 0x53},
    {0x84, 0xFC, 0xFE},
    {0x88, 0x1F, 0xA1},
    {0x88, 0x53, 0x95},
    {0x88, 0x63, 0xDF},
    {0x88, 0xC6, 0x63},
    {0x88, 0xCB, 0x87},
    {0x8C, 0x00, 0x6D},
    {0x8C, 0x29, 0x37},
    {0x8C, 0x2D, 0xAA},
    {0x8C, 0x58, 0x77},
    {0x8C, 0x7B, 0x9D},
    {0x8C, 0x7C, 0x92},
    {0x8C, 0xFA, 0xBA},
    {0x90, 0x27, 0xE4},
    {0x90, 0x72, 0x40},
    {0x90, 0x84, 0x0D},
    {0x90, 0xB2, 0x1F},
    {0x90, 0xB9, 0x31},
    {0x90, 0xFD, 0x61},
    {0x94, 0x94, 0x26},
    {0x98, 0x03, 0xD8},
    {0x98, 0xB8, 0xE3},
    {0x98, 0xD6, 0xBB},
    {0x98, 0xF0, 0xAB},
    {0x98, 0xFE, 0x94},
    {0x9C, 0x04, 0xEB},
    {0x9C, 0x20, 0x7B},
    {0x9C, 0xF3, 0x87},
    {0xA0, 0xED, 0xCD},
    {0xA4, 0x67, 0x06},
    {0xA4, 0xB1, 0x97},
    {0xA4, 0xC3, 0x61},
    {0xA4, 0xD1, 0xD2},
    {0xA8, 0x20, 0x66},
    {0xA8, 0x5B, 0x78},
    {0xA8, 0x86, 0xDD},
    {0xA8, 0x88, 0x08},
    {0xA8, 0x8E, 0x24},
    {0xA8, 0x96, 0x8A},
    {0xA8, 0xBB, 0xCF},
    {0xA8, 0xFA, 0xD8},
    {0xAC, 0x3C, 0x0B},
    {0xAC, 0x7F, 0x3E},
    {0xAC, 0x87, 0xA3},
    {0xAC, 0xCF, 0x5C},
    {0xAC, 0xFD, 0xEC},
    {0xB0, 0x34, 0x95},
    {0xB0, 0x65, 0xBD},
    {0xB0, 0x9F, 0xBA},
    {0xB4, 0x18, 0xD1},
    {0xB4, 0xF0, 0xAB},
    {0xB8, 0x17, 0xC2},
    {0xB8, 0x78, 0x2E},
    {0xB8, 0x8D, 0x12},
    {0xB8, 0xC7, 0x5D},
    {0xB8, 0xE8, 0x56},
    {0xB8, 0xF6, 0xB1},
    {0xB8, 0xFF, 0x61},
    {0xBC, 0x3B, 0xAF},
    {0xBC, 0x52, 0xB7},
    {0xBC, 0x67, 0x78},
    {0xBC, 0x92, 0x6B},
    {0xC0, 0x63, 0x94},
    {0xC0, 0x84, 0x7A},
    {0xC0, 0x9F, 0x42},
    {0xC0, 0xF2, 0xFB},
    {0xC4, 0x2C, 0x03},
    {0xC8, 0x2A, 0x14},
    {0xC8, 0x33, 0x4B},
    {0xC8, 0x6F, 0x1D},
    {0xC8, 0x85, 0x50},
    {0xC8, 0xB5, 0xB7},
    {0xC8, 0xBC, 0xC8},
    {0xC8, 0xE0, 0xEB},
    {0xC8, 0xF6, 0x50},
    {0xCC, 0x08, 0xE0},
    {0xCC, 0x78, 0x5F},
    {0xD0, 0x23, 0xDB},
    {0xD0, 0x4F, 0x7E},
    {0xD0, 0xE1, 0x40},
    {0xD4, 0x9A, 0x20},
    {0xD4, 0xF4, 0x6F},
    {0xD8, 0x00, 0x4D},
    {0xD8, 0x30, 0x62},
    {0xD8, 0x96, 0x95},
    {0xD8, 0x9E, 0x3F},
    {0xD8, 0xA2, 0x5E},
    {0xD8, 0xBB, 0x2C},
    {0xD8, 0xCF, 0x9C},
    {0xD8, 0xD1, 0xCB},
    {0xDC, 0x2B, 0x61},
    {0xDC, 0x86, 0xD8},
    {0xDC, 0x9B, 0x9C},
    {0xE0, 0x66, 0x78},
    {0xE0, 0xB5, 0x2D},
    {0xE0, 0xB9, 0xBA},
    {0xE0, 0xC9, 0x7A},
    {0xE0, 0xF5, 0xC6},
    {0xE0, 0xF8, 0x47},
    {0xE4, 0x25, 0xE7},
    {0xE4, 0x8B, 0x7F},
    {0xE4, 0x98, 0xD6},
    {0xE4, 0xC6, 0x3D},
    {0xE4, 0xCE, 0x8F},
    {0xE8, 0x04, 0x0B},
    {0xE8, 0x06, 0x88},
    {0xE8, 0x80, 0x2E},
    {0xE8, 0x8D, 0x28},
    {0xEC, 0x35, 0x86},
    {0xEC, 0x85, 0x2F},
    {0xF0, 0x24, 0x75},
    {0xF0, 0xB4, 0x79},
    {0xF0, 0xC1, 0xF1},
    {0xF0, 0xCB, 0xA1},
    {0xF0, 0xD1, 0xA9},
    {0xF0, 0xDB, 0xF8},
    {0xF0, 0xDC, 0xE2},
    {0xF0, 0xF6, 0x1C},
    {0xF4, 0x1B, 0xA1},
    {0xF4, 0x37, 0xB7},
    {0xF4, 0xF1, 0x5A},
    {0xF4, 0xF9, 0x51},
    {0xF8, 0x1E, 0xDF},
    {0xF8, 0x27, 0x93},
    {0xFC, 0x25, 0x3F},

    {0x00, 0x18, 0x82}, //huawei
    {0x00, 0x1E, 0x10},
    {0x00, 0x22, 0xA1},
    {0x00, 0x25, 0x68},
    {0x00, 0x25, 0x9E},
    {0x00, 0x46, 0x4B},
    {0x00, 0x66, 0x4B},
    {0x00, 0xE0, 0xFC},
    {0x04, 0xBD, 0x70},
    {0x04, 0xC0, 0x6F},
    {0x04, 0xF9, 0x38},
    {0x08, 0x19, 0xA6},
    {0x08, 0x63, 0x61},
    {0x08, 0x7A, 0x4C},
    {0x08, 0xE8, 0x4F},
    {0x0C, 0x37, 0xDC},
    {0x0C, 0x96, 0xBF},
    {0x10, 0x1B, 0x54},
    {0x10, 0x47, 0x80},
    {0x10, 0x51, 0x72},
    {0x10, 0xC6, 0x1F},
    {0x14, 0xB9, 0x68},
    {0x1C, 0x1D, 0x67},
    {0x1C, 0x8E, 0x5C},
    {0x20, 0x08, 0xED},
    {0x20, 0x0B, 0xC7},
    {0x20, 0x2B, 0xC1},
    {0x20, 0xF3, 0xA3},
    {0x24, 0x09, 0x95},
    {0x24, 0x69, 0xA5},
    {0x24, 0x7F, 0x3C},
    {0x24, 0xDB, 0xAC},
    {0x28, 0x31, 0x52},
    {0x28, 0x3C, 0xE4},
    {0x28, 0x5F, 0xDB},
    {0x28, 0x6E, 0xD4},
    {0x30, 0x87, 0x30},
    {0x30, 0xD1, 0x7E},
    {0x34, 0x00, 0xA3},
    {0x34, 0x6B, 0xD3},
    {0x34, 0xCD, 0xBE},
    {0x38, 0xF8, 0x89},
    {0x3C, 0xDF, 0xBD},
    {0x3C, 0xF8, 0x08},
    {0x40, 0x4D, 0x8E},
    {0x40, 0xCB, 0xA8},
    {0x48, 0x46, 0xFB},
    {0x48, 0x62, 0x76},
    {0x4C, 0x1F, 0xCC},
    {0x4C, 0x54, 0x99},
    {0x4C, 0x8B, 0xEF},
    {0x4C, 0xB1, 0x6C},
    {0x50, 0x9F, 0x27},
    {0x54, 0x39, 0xDF},
    {0x54, 0x89, 0x98},
    {0x54, 0xA5, 0x1B},
    {0x58, 0x1F, 0x28},
    {0x5C, 0x4C, 0xA9},
    {0x5C, 0x7D, 0x5E},
    {0x5C, 0xF9, 0x6A},
    {0x60, 0xDE, 0x44},
    {0x60, 0xE7, 0x01},
    {0x64, 0x16, 0xF0},
    {0x64, 0x3E, 0x8C},
    {0x68, 0xA0, 0xF6},
    {0x70, 0x54, 0xF5},
    {0x70, 0x72, 0x3C},
    {0x70, 0x7B, 0xE8},
    {0x70, 0xA8, 0xE3},
    {0x74, 0x88, 0x2A},
    {0x78, 0x1D, 0xBA},
    {0x78, 0x6A, 0x89},
    {0x78, 0xD7, 0x52},
    {0x78, 0xF5, 0xFD},
    {0x7C, 0x60, 0x97},
    {0x80, 0x71, 0x7A},
    {0x80, 0xB6, 0x86},
    {0x80, 0xD0, 0x9B},
    {0x80, 0xFB, 0x06},
    {0x84, 0xA8, 0xE4},
    {0x84, 0xDB, 0xAC},
    {0x88, 0x53, 0xD4},
    {0x88, 0x86, 0x03},
    {0x88, 0xCE, 0xFA},
    {0x88, 0xE3, 0xAB},
    {0x90, 0x17, 0xAC},
    {0x90, 0x4E, 0x2B},
    {0x9C, 0x28, 0xEF},
    {0x9C, 0xC1, 0x72},
    {0xA4, 0x99, 0x47},
    {0xAC, 0x4E, 0x91},
    {0xAC, 0x85, 0x3D},
    {0xAC, 0xE2, 0x15},
    {0xAC, 0xE8, 0x7B},
    {0xB0, 0x5B, 0x67},
    {0xB4, 0x15, 0x13},
    {0xB4, 0x30, 0x52},
    {0xBC, 0x76, 0x70},
    {0xC0, 0x70, 0x09},
    {0xC4, 0x05, 0x28},
    {0xC8, 0xD1, 0x5E},
    {0xCC, 0x53, 0xB5},
    {0xCC, 0x96, 0xA0},
    {0xCC, 0xA2, 0x23},
    {0xCC, 0xCC, 0x81},
    {0xD0, 0x2D, 0xB3},
    {0xD0, 0x7A, 0xB5},
    {0xD4, 0x6A, 0xA8},
    {0xD4, 0x6E, 0x5C},
    {0xD4, 0xB1, 0x10},
    {0xD8, 0x49, 0x0B},
    {0xDC, 0xD2, 0xFC},
    {0xE0, 0x19, 0x1D},
    {0xE0, 0x24, 0x7F},
    {0xE0, 0x97, 0x96},
    {0xE4, 0x68, 0xA3},
    {0xE8, 0x08, 0x8B},
    {0xE8, 0xCD, 0x2D},
    {0xEC, 0x23, 0x3D},
    {0xEC, 0xCB, 0x30},
    {0xF4, 0x55, 0x9C},
    {0xF4, 0x9F, 0xF3},
    {0xF4, 0xC7, 0x14},
    {0xF4, 0xDC, 0xF9},
    {0xF8, 0x01, 0x13},
    {0xF8, 0x3D, 0xFF},
    {0xF8, 0x4A, 0xBF},
    {0xF8, 0xE8, 0x11},
    {0xFC, 0x48, 0xEF},

    {0x00, 0x9E, 0xC8}, //xiaomi
    {0x0C, 0x1D, 0xAF},
    {0x14, 0xF6, 0x5A},
    {0x28, 0xE3, 0x1F},
    {0x64, 0x09, 0x80},
    {0x64, 0xB4, 0x73},
    {0x68, 0xDF, 0xDD},
    {0x7C, 0x1D, 0xD9},
    {0x8C, 0xBE, 0xBE},
    {0x98, 0xFA, 0xE3},
    {0xAC, 0xF7, 0xF3},
    {0xC4, 0x6A, 0xB7},
    {0xD4, 0x97, 0x0B},
    {0xF8, 0xA4, 0x5F},

    {0x18, 0xDC, 0x65}, //coolpad
    {0x18, 0xDC, 0x56}};
CONST uint8_t hid_descriptor[] = {
    0x35, 204,
    0x35, 202,
    0x08, 0x22,
    0x25, 198,
    //---------------
    0x05, 0x01,       // Usage Page (Generic Desktop),
    0x09, 0x06,       // Usage (Keyboard),
    0xA1, 0x01,       // Collection (Application),
    0x85, 0x01,       // Report ID
    0x05, 0x07,       // Usage Page (Key Codes);
    0x19, 0xE0,       // Usage Minimum (224),
    0x29, 0xE7,       // Usage Maximum (231),
    0x15, 0x00,       // Logical Minimum (0),
    0x25, 0x01,       // Logical Maximum (1),
    0x75, 0x01,       // Report Size (1),
    0x95, 0x08,       // Report Count (8),
    0x81, 0x02,       // Input (Data, Variable, Absolute), ;Modifier byte
    0x95, 0x01,       // Report Count (1),
    0x75, 0x08,       // Report Size (8),
    0x81, 0x03,       // Input (Constant), ;Reserved byte
    0x95, 0x05,       // Report Count (5),
    0x75, 0x01,       // Report Size (1),
    0x05, 0x08,       // Usage Page (Page# for LEDs),
    0x19, 0x01,       // Usage Minimum (1),
    0x29, 0x05,       // Usage Maximum (5),
    0x91, 0x02,       // Output (Data, Variable, Absolute), ;LED report
    0x95, 0x01,       // Report Count (1),
    0x75, 0x03,       // Report Size (3),
    0x91, 0x03,       // Output (Constant), ;LED report padding
    0x95, 0x06,       // Report Count (6),
    0x75, 0x08,       // Report Size (8),
    0x15, 0x00,       // Logical Minimum (0),
    0x26, 0xff, 0x00, // Logical Maximum(255),
    0x05, 0x07,       // Usage Page (Key Codes),
    0x19, 0x00,       // Usage Minimum (0),
    0x29, 0xff,       // Usage Maximum (255),
    0x81, 0x00,       // Input (Data, Array), ;Key arrays (6 bytes)
    0xC0,             // End Collection

    //-----------multi-media control-------------
    0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
    0x09, 0x01, // USAGE (Consumer Control)
    0xa1, 0x01, // COLLECTION (Application)
    0x85, 0x03, // Report ID (3)
    0x15, 0x00, // LOGICAL_MINIMUM (0)
    0x25, 0x01,
    0x75, 0x01,
    0x95, 0x1e,
    0x0a, 0x24, 0x02, //WWW back  0
    0x0a, 0x25, 0x02, //WWW forward 1
    0x0a, 0x26, 0x02, //WWW Stop  2
    0x0a, 0x27, 0x02, //WWW Refresh 3
    0x0a, 0x21, 0x02, //WWW Search 4
    0x0a, 0x2A, 0x02, //WWW Favorites  5
    0x0a, 0x23, 0x02, //WWW Home 6
    0x0a, 0x8A, 0x01, //Mail  7
    0x09, 0xE2,       //Mute 8
    0x09, 0xEA,       //Volume-  9
    0x09, 0xE9,       //Volume+   10
    0x09, 0xCD,       //Play/Pause  11
    0x09, 0xB7,       //Stop  12
    0x09, 0xB6,       //Prev Track 13
    0x09, 0xB5,       //Next Track  14
    0x0a, 0x83, 0x01, //Media Select  15
    0x0a, 0x94, 0x01, //My Computer   16
    0x0a, 0x92, 0x01, //Calculator   17
    0x0a, 0x09, 0x02, //More Info  18
    0x09, 0xB2,       //Record     19
    0x09, 0xB3,       //Forward     20
    0x09, 0xB4,       //Rewind      21
    0x09, 0x8D,       //Guide        22
    0x09, 0x04,       //<Reserved>   23
    0x09, 0x30,       //Eject(Mac,power)   24
    0x0a, 0x07, 0x03, //H7    31touchkey
    0x0a, 0x0A, 0x03, //H10	34lightness+
    0x0a, 0x0B, 0x03, //H11	35lightness-
    0x0a, 0xb1, 0x01, //photo 45
    0x09, 0xb8,       //touchkey 46
    0x81, 0x02,
    0x95, 0x01, // REPORT_COUNT (1)
    0x75, 0x02,
    0x81, 0x03,
    0xc0,       // END_COLLECTION
                //====================power supply control============
    0x05, 0x01, // Usage Page (Generic Desktop),
    0x09, 0x80, // USAGE (3-D Digitizer)
    0xa1, 0x01, // Collection (Application),
    0x85, 0x04, // Report ID (4)
    0x05, 0x01, // Usage Page (Generic Desktop),
    0x19, 0x81, // Usage Minimum (),
    0x29, 0x83, // Usage Maximum (),
    0x15, 0x00, // Logical Minimum (0),
    0x25, 0x01, // Logical Maximum (1),
    0x95, 0x03, // Report Count (3),
    0x75, 0x01, // Report Size (1),
    0x81, 0x06, // Input (Data,Var,Rel)
    0x95, 0x01, // Report Count (1),
    0x75, 0x05, // Report Size (5),
    0x81, 0x01, // Input (Const,Ary,Abs)
    0xc0,       // END_COLLECTION

};

uint8_t enterKey_press[] =
    {0x01, //report id
     0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t volumeKey_press[] =
    {0x03,
     0x00, 0x04, 0x00, 0x00};

uint8_t enterKey_releas[] =
    {0x01, //report id
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t volumeKey_releas[] =
    {0x03,
     0x00, 0x00, 0x00, 0x00};

result_t bt_hid_ctrl_conn_connect(btaddr_t *laddr, btaddr_t *raddr);

uint16_t getDescriptorLength(void)
{
    return sizeof(hid_descriptor);
}

result_t hid_backend_init(void)
{
    result_t err;

    err = hid_init();
    if (err)
    {
        return err;
    }

    err = bt_sdp_service_register(BTADDR_ANY,
                                  BT_SVC_TYPE_HUMAN_INTERFACE_DEVICE,
                                  &hid_profile_ct,
                                  sizeof(hid_profile_ct),
                                  testHandle);
    hid_mode = 0;
    return err;
}

result_t hid_cmd_connect(char *params, unsigned int len)
{
    result_t err;
    btaddr_t laddr, raddr;
    unsigned int i, unit_id, tmp[6];

    if (j_snscanf(params, NULL, "%u " BTADDR_FORMAT, &unit_id, &tmp[5], &tmp[4],
                  &tmp[3], &tmp[2], &tmp[1], &tmp[0]) != 7)
    {
        return UWE_PARAM;
    }

    for (i = 0; i < 6; i++)
        raddr.b[i] = (uint8_t)tmp[i];

    err = backend_unit_get_addr(unit_id, &laddr);
    if (err)
        return err;

    g_app_hid.svc_id++;

    err = bt_hid_ctrl_conn_connect(&laddr, &raddr);
    if (err)
        goto Exit;

    os_printf("hid_reconn\r\n");

Exit:
    return err;
}

result_t hid_cmd_disconnect(void)
{
    return hid_int_conn_disconnect();
}

void hid_disconnect_task(void)
{
    if (app_bt_flag1_get(APP_FLAG_HID_CONNECTION))
        hid_cmd_disconnect();
}

void hid_con_connected(uint8_t flag)
{
    //   app_handle_t sys_hdl = app_get_sys_handler();
    if (hid_mode == 1) //HID need connect first
    {
        if (flag == 1) // if loudspeaker take the conection, won't disconnect
        {
            os_printf("int_hid_connected\r\n");
            app_bt_flag1_set(APP_FLAG_HID_CONNECTION, 1);
        }
        else
        {
            hid_cmd_disconnect(); // if phones take the connection, will disconnect HID
                                  //schedule to disconnect
                                  /*		jtask_schedule(sys_hdl->app_a2dp_task, 
                        500,
                        hid_disconnect_task, 
                        NULL);*/
        }
    }
    else //HID always connects
    {
        os_printf("int_hid_connected2\r\n");
        app_bt_auto_conn_ok();
        app_bt_flag1_set(APP_FLAG_HID_CONNECTION, 1);
    }
}

// this function need app_bt_con_compl_wrap() get remote device address first
void Set_photoKey(void)
{
    app_handle_t app_h;
    uint16_t tmp, size;
    app_h = app_get_sys_handler();
    size = sizeof(MA_List) / 3;
    tmp = 0;

    photo_key = KEY_ENTER;

    while (tmp < size)
    {
        if ((app_h->remote_btaddr.b[5] == MA_List[tmp][0]) &&
            (app_h->remote_btaddr.b[4] == MA_List[tmp][1]) &&
            (app_h->remote_btaddr.b[3] == MA_List[tmp][2]))
            break;
        tmp++;
    }

    if (tmp < size)
    {
        os_printf("KEY_VOL_INC\r\n");
        photo_key = KEY_VOL_INC;
    }
}

result_t send_Hid_key_press(void)
{
    if (photo_key == KEY_ENTER)
        send_hid_data(enterKey_press, sizeof(enterKey_press), HID_INT_CHANNEL);
    else
        send_hid_data(volumeKey_press, sizeof(volumeKey_press), HID_INT_CHANNEL);
    return 0;
}
result_t send_Hid_key_releas(void)
{
    if (photo_key == KEY_ENTER)
        send_hid_data(enterKey_releas, sizeof(enterKey_releas), HID_INT_CHANNEL);
    else
        send_hid_data(volumeKey_releas, sizeof(volumeKey_releas), HID_INT_CHANNEL);
    return 0;
}

void photo_Key_Atvice(void)
{
    photo_key = KEY_ENTER;
    send_Hid_key_press();
    Delay(100);
    send_Hid_key_releas();

    Delay(200);
    photo_key = KEY_VOL_INC;
    send_Hid_key_press();
    Delay(100);
    send_Hid_key_releas();
}
